
create trigger AtualizaStatusEmTbPropostasPendente
on TbPropostasPendente
after insert
as
begin

begin TRANSACTION

insert into TbHistoricoStatusProposta (NuProposta , StatusProposta, DtStatusProposta)
SELECT a.NuProposta, 'P' as StatusProposta , getdate() as DtStatusProposta FROM TbPropostasPendente a  where a.CdHistoricoStatus is null;


update TbPropostasPendente  set CdHistoricoStatus = b.CdHistoricoStatus
  FROM TbPropostasPendente a
  inner Join TbHistoricoStatusProposta b
  on a.NuProposta = b.NuProposta
  where a.CdHistoricoStatus  is null;

commit

end
go 



/*drop trigger AtualizaStatusEmTbPropostasPendente*/


/*Passo 2  

  

select *
  FROM TbPropostasPendente a
  inner Join TbHistoricoStatusProposta b
  on a.NuProposta = b.NuProposta
  where a.CdHistoricoStatus  is null  
  */
  
  
  /*
delete from [bancopan].[dbo].[TbPropostasPendente] where NuProposta = '300336478'
delete from [bancopan].[dbo].TbHistoricoStatusProposta where NuProposta = '300336478'

update [bancopan].[dbo].[TbPropostasPendente] set CdHistoricoStatus = null where NuProposta = '300336478'

delete from [bancopan].[dbo].[TbPropostaNegociacao] where NuProposta = '300336478'
*/